(function() {
    'use strict';

    /**
     * @ngdoc directive
     * @name webApp.directive:tabBarSetting
     * @description
     * # tabBarSetting
     */
    angular.module('webApp')
        .run(["$templateCache", "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.tabbarsetting.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive('tabBarSetting', ["$rootScope", "helper", function($rootScope, helper) {
            return {
                restrict: 'E',
                template: "<span ng-click='show()'>{{title}}</span>",
                replace: true,
                require: 'ngModel',
                scope: {
                    ngModel: "=",
                    callback: "&", //确认回调函数
                    title: "@",
                    templet: "="
                },
                controller: ['$scope', 'authService', 'sessionService', 'dataService', '$compile', 'helper', '$templateCache', 'base64', '$timeout', 'ERP_CONFIG', function($scope, authService, sessionService, dataService, $compile, helper, $templateCache, base64, $timeout, ERP_CONFIG) {
                    // $scope.data = {
                    //     "color": "#999999",
                    //     "selectedColor": "#479de6",
                    //     "borderStyle": "#f3f3f3",
                    //     "backgroundColor": "#fff",
                    // };
                    $scope.title = $scope.title || "按钮";

                    //                     console.error("ngModel", $scope.ngModel);
                    $scope.iconPathChange = function(data) {
                        $timeout(function() {
                            if ($scope.ngModel.tabBar.list.length == 4) {
                                $scope.ngModel.tabBar.list[0].iconPath = "assets/images/home" + $scope.iconPath + ".png";
                                $scope.ngModel.tabBar.list[1].iconPath = "assets/images/cart" + $scope.iconPath + ".png";
                                $scope.ngModel.tabBar.list[2].iconPath = "assets/images/order" + $scope.iconPath + ".png";
                                $scope.ngModel.tabBar.list[3].iconPath = "assets/images/my" + $scope.iconPath + ".png";
                            }

                            if ($scope.ngModel.tabBar.list.length == 2) {
                                $scope.ngModel.tabBar.list[0].iconPath = "assets/images/home" + $scope.iconPath + ".png";
                                $scope.ngModel.tabBar.list[1].iconPath = "assets/images/my" + $scope.iconPath + ".png";
                            }
                        })
                    }

                    $scope.selectedIconPathChange = function() {
                        $timeout(function() {
                            if ($scope.ngModel.tabBar.list.length == 4) {
                                $scope.ngModel.tabBar.list[0].selectedIconPath = "assets/images/home" + $scope.selectedIconPath + ".png";
                                $scope.ngModel.tabBar.list[1].selectedIconPath = "assets/images/cart" + $scope.selectedIconPath + ".png";
                                $scope.ngModel.tabBar.list[2].selectedIconPath = "assets/images/order" + $scope.selectedIconPath + ".png";
                                $scope.ngModel.tabBar.list[3].selectedIconPath = "assets/images/my" + $scope.selectedIconPath + ".png";
                            }

                            if ($scope.ngModel.tabBar.list.length == 2) {
                                $scope.ngModel.tabBar.list[0].selectedIconPath = "assets/images/home" + $scope.selectedIconPath + ".png";
                                $scope.ngModel.tabBar.list[1].selectedIconPath = "assets/images/my" + $scope.selectedIconPath + ".png";
                            }
                        });
                    }
                    $scope.deleteTabBarItem = function(index) {
                        $scope.ngModel.tabBar.list.splice(index, 1);
                    }

                    $scope.addTabBarItem = function() {
                        var data = {
                            selectedIconPath: $scope.ngModel.tabBar.list[0] ? $scope.ngModel.tabBar.list[0].selectedIconPath : "assets/images/home3388FF.png",
                            iconPath: $scope.ngModel.tabBar.list[0] ? $scope.ngModel.tabBar.list[0].iconPath : "assets/images/home999999.png",
                            pagePath: "pages/index/index",
                            text: "新增1"
                        }
                        if (!$scope.ngModel.tabBar.list) {
                            $scope.ngModel.tabBar.list = [];
                        }

                        if (_.size($scope.ngModel.tabBar.list) < 5) {

                            $scope.ngModel.tabBar.list.push(data);
                        } else {
                            helper.myAlert("Up to 5", null, 5);
                        }
                        console.error("$scope.ngModel.tabBar.list", $scope.ngModel.tabBar.list);
                    }
                    $scope.show = function() {
                        //console.error("11111", $scope.ngModel.tabBar, $scope.ngModel.topBar);
                        //弹出
                        $scope.loginWin = $.alert({
                            backdrop: "static",
                            keyboard: false,
                            title: '<span>{{"小程序窗口设置"|translate}}</span>',
                            body: $templateCache.get("views/directives.tabbarsetting.html"),
                            okBtn: '{{"确认"|translate}}',
                            closeBtn: true,
                            hasfoot: true,
                            transition: true,
                            width: '600',
                            height: '400',
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                            },
                            okHide: function(e) {
                                if (!$scope.ngModel.tabBar.list) {

                                    helper.myAlert("底部导航不能为空");

                                    return false;
                                } else {
                                    if(!$scope.ngModel.navigateToMiniProgramAppIdList){
                                       
                                        $scope.ngModel.navigateToMiniProgramAppIdList =["wx6885acbedba59c14"];
                                    }
                                    
                                    $scope.callback({ templet: $scope.templet });
                                }

                            },
                            shown: function(e) {

                            }
                        });
                    }


                }],

                link: function(scope, elem, attrs, ctrl) {
                    //初始化，获取数据
                    ctrl.$render = function() {
                        if (ctrl.$viewValue && ctrl.$viewValue.topBar && ctrl.$viewValue.tabBar) {
                            // console.error(111111, ctrl.$viewValue)
                            ctrl.$setViewValue(ctrl.$viewValue);
                        }
                    };
                }
            }
        }])
        .run(["$templateCache", "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.jumpapplist.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive('jumpAppList', ["$rootScope", "helper", function($rootScope, helper) {
            return {
                restrict: 'E',
                template: "<span ng-click='init()'>{{title}}</span>",
                replace: true,
                require: 'ngModel',
                scope: {
                    ngModel: "=",
                    callback: "&", //确认回调函数
                    title: "@",
                    templet: "="
                },
                controller: ['$scope', 'authService', 'sessionService', 'dataService', '$compile', 'helper', '$templateCache', 'base64', '$timeout', 'ERP_CONFIG', function($scope, authService, sessionService, dataService, $compile, helper, $templateCache, base64, $timeout, ERP_CONFIG) {

                    $scope.title = $scope.title || "按钮";

                    $scope.init = function() {
                        $scope.getData().then($scope.show);
                    }

                    $scope.deleteAppid = function(index) {
                        $scope.windowConfig.navigateToMiniProgramAppIdList.splice(index, 1);
                    }

                    $scope.addAppid = function() {
                        if (!$scope.windowConfig.navigateToMiniProgramAppIdList) {
                            $scope.windowConfig.navigateToMiniProgramAppIdList = [];
                        }
                        
                        if ($scope.windowConfig.navigateToMiniProgramAppIdList.indexOf($scope.addappid) == '-1') {
                            if (_.size($scope.windowConfig.navigateToMiniProgramAppIdList) < 10) {
                                $scope.windowConfig.navigateToMiniProgramAppIdList.push($scope.addappid);
                            } else {
                                helper.myAlert("最多只能增加 10个小程序");
                                return false;
                            }
                        }else{
                            helper.myAlert("此小程序已经添加！");
                            return false;
                        }
                        
                        console.error("$scope.ngModel.tabBar.list", $scope.windowConfig);
                    }
                    $scope.getData = function() {
                        $scope.windowConfig = {};
                        return dataService.shopConfigOneByKey("WXAPP_UI_CONFIG_" + $scope.ngModel).then(function(conf) {
                            if (conf) {
                                $scope.config = conf;
                                if (conf.value) {
                                    $scope.windowConfig = JSON.parse(conf.value);
                                    $scope.addtsAppid();
                                }
                            }
                        });
                    }

                    $scope.addtsAppid = function() {
                        if (!$scope.windowConfig.navigateToMiniProgramAppIdList) {
                            $scope.windowConfig.navigateToMiniProgramAppIdList = [];
                            $scope.windowConfig.navigateToMiniProgramAppIdList.push('wx6885acbedba59c14');
                        } else {
                            if ($scope.windowConfig.navigateToMiniProgramAppIdList.indexOf('wx6885acbedba59c14') == '-1') {
                                $scope.windowConfig.navigateToMiniProgramAppIdList.push('wx6885acbedba59c14');
                            }
                        }
                        console.error("$scope.windowConfig",$scope.windowConfig.navigateToMiniProgramAppIdList.indexOf('wx6885acbedba59c14'));
                    }

                    $scope.show = function() {
                        //弹出
                        $scope.loginWin = $.alert({
                            backdrop: "static",
                            keyboard: false,
                            title: '<span>{{"此小程序跳转到其他小程序设置"|translate}}</span>',
                            body: $templateCache.get("views/directives.jumpapplist.html"),
                            okBtn: '{{"确认"|translate}}',
                            closeBtn: true,
                            hasfoot: true,
                            transition: true,
                            width: '600',
                            height: '400',
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                            },
                            okHide: function(e) {
                                $scope.Update();
                            },
                            shown: function(e) {

                            }
                        });
                    }

                    $scope.Update = function() {
                        $scope.config.value = clone($scope.windowConfig);
                        console.error("$scope.config", $scope.config);
                        dataService.shopConfigUpdate($scope.config).then(function() {
                            $scope.callback();
                        })
                    }
                }],

                link: function(scope, elem, attrs, ctrl) {

                }
            }
        }]);
})();